package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.dto.TestProductDetailDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.ITestProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @projectName: duolaimall
 * @package: com.cskaoyan.shopping.controller
 * @className: ShoppingCartController
 * @author: 杨浩祥
 * @description: TODO
 * @date: 2022/10/25 20:30
 * @version: 1.0
 */
@RestController
public class IShoppingCartController {


    @Autowired
    ICartService cartService;

    @PostMapping("/shopping/carts")
    public ResponseData addProductToCart(@Valid @RequestBody CartRequest cartRequest) {
        cartRequest.setChecked("true");
        cartService.addToCart(cartRequest);
        return new ResponseUtil().setData("成功","success");
    }

    @PutMapping("/shopping/carts")
    public ResponseData updateCartNum(@Valid @RequestBody CartRequest cartRequest) {
        cartService.updateCartNum(cartRequest);
        return new ResponseUtil().setData("成功","success");
    }

    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") long uid,@PathVariable("pid") long pid){
        cartService.deleteCartItem(uid,pid);
        return new ResponseUtil().setData("成功","success");
    }
    @GetMapping("/shopping/carts")
    public ResponseData showCart(HttpServletRequest request) {
        // 获取用户的Id
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        List<CartProductDto> cartListById = cartService.getCartListById(uid);
        return new ResponseUtil().setData(cartListById);
    }
    @PutMapping("/shopping/items")
    public ResponseData allChoose(@RequestBody CheckAllItemRequest checkAllItemRequest) {
        cartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil().setData("成功");
    }
}
